package com.ruoyi.web.controller.aggregation;

import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.aggregation.Point;
import com.ruoyi.system.domain.aggregation.Reward;
import com.ruoyi.system.mapper.aggregation.PointMapper;
import com.ruoyi.system.mapper.aggregation.RewardMapper;
import com.ruoyi.system.service.aggregation.ICommissionService;

/**
 * 回调接口Controller
 * 
 * @author ruoyi
 * @date 2025-01-01
 */
@RestController
@RequestMapping("/agg/callback")
public class CallbackController
{
    @Autowired
    private RewardMapper rewardMapper;

    @Autowired
    private PointMapper pointMapper;

    @Autowired
    private ICommissionService commissionService;

    /**
     * 穿山甲异步通知
     */
    @PostMapping("/pangolin")
    public AjaxResult pangolinCallback(@RequestBody Reward reward)
    {
        try
        {
            // 保存奖励记录
            reward.setType("1"); // 穿山甲
            reward.setCreateTime(new Date());
            rewardMapper.insertReward(reward);

            // 触发广告分佣
            commissionService.calculateAdCommission(reward.getUserId(), 
                Long.valueOf(reward.getRewardAmount()), "1");

            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 塔库异步通知
     */
    @PostMapping("/taku")
    public AjaxResult takuCallback(@RequestBody Reward reward)
    {
        try
        {
            // 保存奖励记录
            reward.setType("2"); // taku
            reward.setCreateTime(new Date());
            rewardMapper.insertReward(reward);

            // 触发广告分佣
            commissionService.calculateAdCommission(reward.getUserId(), 
                Long.valueOf(reward.getRewardAmount()), "2");

            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 支付宝异步通知
     */
    @PostMapping("/alipay")
    public AjaxResult alipayCallback(@RequestBody String notifyData)
    {
        try
        {
            // TODO: 验证支付宝签名
            // TODO: 解析支付宝通知数据
            // TODO: 更新订单状态或提现状态
            
            System.out.println("收到支付宝异步通知：" + notifyData);
            
            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}

